/**
 *   客户创建后添加默认科室和联系人
 *   created by Harry 2016-11-17
 */
public without sharing class AccSetDefaultDeptAndContactHandler implements Triggers.Handler{
    public void handle(){
        //本次生成的部门名称
        Map<String,Department__c> mapDeptNames = new Map<String,Department__c>();
        List<Contact> preContList = new List<Contact>();
        Map<Department__c,Id> daMap = new Map<Department__c,Id>();
        Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Account.getRecordTypeInfosById();

        for (Account acc : (List<Account>)Trigger.new){
            if(rtMap.get(acc.RecordTypeId).getName()=='终端客户'){//终端客户
                List<Department__c> deptList = genDeptList(acc);
                for(Department__c dp : deptList){
                    //记录要新增的部门名称
                    Integer len = dp.hospitalDepartments__c.length() ;
                    String name = acc.Name;
                    if(name.length() > 250 - len){
                        name = name.substring(0, 250-len);
                    }
                    name = name + dp.hospitalDepartments__c;
                    if(!mapDeptNames.containsKey(name)){
                        mapDeptNames.put(name,dp);
                    }
                }
                preContList.addAll(genContact(acc));
            }
            if(rtMap.get(acc.RecordTypeId).getName()=='经销商'){//经销商
                List<Contact> dealerContactList = newContactList(acc);
                preContList.addAll(dealerContactList);
            }
        }
        //去掉系统中已存在的部门
        List<Department__c> existList = [SELECT Id,DepartmentNameUnique__c FROM Department__c WHERE DepartmentNameUnique__c IN :mapDeptNames.keySet()];
        for(Department__c el : existList){
            mapDeptNames.remove(el.DepartmentNameUnique__c);
        }
        insert mapDeptNames.values();//持久化科室对象
        insert preContList;//持久化联系人对象 
    }

    //终端客户生成4个科室,   检验科，医院办公室，财务科/计财处，设备科
    private List<Department__c> genDeptList(Account acc){
        List<Department__c> departmentList = new List<Department__c>();
        departmentList.add(genDepartment('检验科',acc));
        System.debug(LoggingLevel.INFO, '*** departmentList: ' + departmentList);
        return departmentList;
    }

    //根据科室类型和医院名称创建科室
    private Department__c genDepartment(String deptType,Account acc){
        Department__c dept = new Department__c(TerminalCustomer__c = acc.Id, hospitalDepartments__c=deptType);
        Integer len = deptType.length() ;
        String name = acc.Name;
        if(name.length() > 80 - len){
            name = name.substring(0,80 - len);
        }
        dept.Name = name + deptType;
        System.debug(LoggingLevel.INFO, '*** 部门名称dept.Name=: ' + dept.Name);
        return dept;
    }
    //终端客户生成3个联系人，带选项列表的
    private List<Contact> genContact(Account acc){
        List<Contact> contactList = new List<Contact>();
        Id recordTypeID = Utils.getRecordTypeIdByDevNameAndObjName('终端客户联系人', 'Contact');
        contactList.add(new Contact(LastName='默认联系人1',Post__c='科长/主任',Departments__c='检验科',AccountId=acc.Id,RecordTypeId=recordTypeID));
        contactList.add(new Contact(LastName='默认联系人2',Post__c='院长',Departments__c='医院办公室',AccountId=acc.Id,RecordTypeId=recordTypeID));
        contactList.add(new Contact(LastName='默认联系人3',Post__c='科长/主任',Departments__c='设备科/工程部/维修科',AccountId=acc.Id,RecordTypeId=recordTypeID));
        return contactList;
    }

    //经销商创建联系人
    private List<Contact> newContactList(Account acc){
        List<Contact> contactList = new List<Contact>();
        Id rocordTypeID = Utils.getRecordTypeIdByDevNameAndObjName('经销商联系人', 'Contact');
        contactList.add(new Contact(LastName='默认联系人1',Post__c='总经理',AccountId=acc.Id,RecordTypeId=rocordTypeID));
        contactList.add(new Contact(LastName='默认联系人2',Post__c='财务',AccountId=acc.Id,RecordTypeId=rocordTypeID));
        return contactList;
    }
}